<?php

namespace Event\Drive;

use Event\Drive\Contracts\ValidatorContract;

class EventParamHandle
{
    /**
     * 校验事件参数
     * @param string $eventClassName
     * @param array $params
     * @return array
     */
    public function check(string $eventClassName, array $params) : array
    {
        return $this->getValidator($eventClassName)->check($params);
    }

    private function getValidator(string $eventClassName) : ValidatorContract
    {
        if (class_exists($validator = $this->getValidatorClass($eventClassName))) {
            return new $validator($eventClassName);
        }
        $defaultValidator = __NAMESPACE__.'\\Validators\\DefaultEventValidator';
        return new $defaultValidator($eventClassName);
    }

    private function getValidatorClass(string $eventClassName)
    {
        return __NAMESPACE__.'\\Validators\\'.class_basename($eventClassName).'Validator';
    }
}
